<?php
session_start();
    if(!isset($_SESSION['pwd']) || empty($_SESSION['pwd']))
    {
        header("Location: login.php"); 
        exit;
    }
if(isset($_REQUEST['onsub'])){
	if( $_REQUEST['onsub']=='ok')
	{
		  //require_once dirname(__FILE__) . '/../includes/common.inc.php';
		  require_once dirname(__FILE__) . '/../includes/data.cfg.php';
		  require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';
		
		  $export_name = BRANCH.$_REQUEST['begin_date'].'--'.$_REQUEST['end_date'].'expense';
		  header("Content-type:application/vnd.ms-excel");
		  header("Content-Disposition:attachment;filename=$export_name.xls");
		
		
		  //require_once dirname(__FILE__) . '/includes/pdoorm.inc.php';
		  
		  
		  $sql="SELECT LEFT(FROM_UNIXTIME(payDate),10) as payDate, LEFT(FROM_UNIXTIME(createdDate),10) AS createdDate,payee,particular,amount,method,chequeNumber,ng,ne,note FROM ns_expense WHERE `status` != '-1' AND LEFT(FROM_UNIXTIME(payDate),10) BETWEEN '".$_REQUEST['begin_date']."' and '".$_REQUEST['end_date']."' ORDER BY payDate";
		  $dataarr = getall($sql,1);
		  echo"<table border='1'><tr>";
		  echo " <td colspan='10' height='30px' align='center'><h1>".$export_name."</h1></td></tr><tr height='27' style='font-weight:bold;'>";
		  echo   "<td>payDate</td>";
		  echo   "<td>createdDate</td>";
		  echo   "<td>payee</td>";
		  echo   "<td>particular</td>";
		  echo   "<td>amount</td>";
		  echo   "<td>method</td>";
		  echo   "<td>Q/N</td>";
		  echo   "<td>NG</td>";
		  echo   "<td>NE</td>";
		  echo   "<td>note</td>";
		  echo  "</tr>";  
		
		  if(is_array($dataarr))
		  {
		    foreach ($dataarr as $dr)
		    {
		    	$ng = $dr['ng']==0? 'N':'Y';
		    	$ne = $dr['ne']==0? 'N':'Y';
		    	
		       echo" <tr>";        
		       echo   "<td>".$dr['payDate']."</td>";
		       echo   "<td>".$dr['createdDate']."</td>";
		       echo   "<td>".$dr['payee']."</td>";
		       echo   "<td>".$dr['particular']."</td>";
		       echo   "<td>".$dr['amount']."</td>";
		       echo   "<td>".$dr['method']."</td>";
		       echo   "<td>".$dr['chequeNumber']."</td>";
		       echo   "<td>".$ng."</td>";
		       echo   "<td>".$ne."</td>";
		       echo   "<td>".$dr['note']."</td>"; 
		       echo  "</tr>";      
		    }
		  }
		  echo"</table>";
		  die;
	}

}
include 'header.php';
//header("Content-type: text/html; charset=utf-8");

?>
 <br>
 <div>
    <fieldset>
    <legend>Expense</legend>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
        <label><span style="color:red;font-weight:bolder;">Date format:2010-01-01</span></label><br>
        <label>P/Date<input type="text" name ="begin_date" id="begin_date"></label><label> TO <input type="text" name ="end_date" id="end_date"></label>
        <label><input type="hidden" name="onsub" id="onsub"  value="ok">
        <input type="submit" value="download"></label>
      </form>
    </fieldset>
    </fieldset>
 </div>